package com.partner.boot.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.fill.Column;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import java.util.Collections;
import java.util.Properties;

/**
 * @author 刘孟坤
 * @description: 代码生成器
 * @Param:
 * @return: null
 * @date: 2023/7/13 22:09
 */
@Slf4j
public class CodeGenerator {

    private static final String PACKAGE_NAME = "com.partner.boot";

    private static final String AUTHOR = "刘孟坤";

    private static final String TABLE_NAME = "dynamic";

    private static final String PROJECT_PATH= System.getProperty("user.dir");

    private static final String MAPPER_XML_PATH="/partner-back/src/main/resources/mapper/";

    private static final String JAVA_CODE_PATH="/partner-back/src/main/java/";

    public static void main(String[] args) {
        generateJava(TABLE_NAME);
    }

    private static void generateJava(String tableName){
        ClassPathResource resource = new ClassPathResource("application.yml");
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(resource);
        Properties properties = yamlPropertiesFactoryBean.getObject();
        FastAutoGenerator.create(properties.getProperty("spring.datasource.url"),
                properties.getProperty("spring.datasource.username"),
                properties.getProperty("spring.datasource.password"))
                .globalConfig(builder -> {
                    //设置作者
                    builder.author(AUTHOR)
                            .enableSwagger()
                            .disableOpenDir()
                            //指定输出目录
                            .outputDir(PROJECT_PATH + JAVA_CODE_PATH);
                })
                .packageConfig(builder -> {
                    //设置父包名
                    builder.parent(PACKAGE_NAME)
                            //设置父模块名
                            .moduleName("")
                            .pathInfo(Collections.singletonMap(OutputFile.xml,PROJECT_PATH+MAPPER_XML_PATH));
                })
                .strategyConfig(builder -> {
                    builder.controllerBuilder().fileOverride().enableRestStyle().enableHyphenStyle()
                            .serviceBuilder().fileOverride().mapperBuilder().fileOverride().entityBuilder()
                            .fileOverride().enableLombok().addTableFills(
                                    new Column("create_time", FieldFill.INSERT));
                    builder
                            //设置生成的表明
                            .addInclude(tableName)
                            //设置表前缀
                            .addTablePrefix("t_","sys_");
                })
                .templateEngine(new FreemarkerTemplateEngine()).execute();
    }
}
